#ifndef TSFILE_H
#define TSFILE_H

#include "table.h"
#include "section.h"
#include "packet.h"
#include "file.h"

using namespace NDVBSpace;

class CTSFile :public CFile
{
public:
#define PACKETS_NUMBER_TO_TRY_PACKET_SIZE	20
#define INVALID_PACKET_INDEX                    0xFFFFFFFF

#define MAX_TS_PAGE_SIZE  (1000 * 1024 * 1024)  /* 1G */

	enum TSFileState
	{
		TSFileUnknow,
		TSFileOpenFailed,
		TSFileAnalyseFailed,
		TSFileOK,

	};

#define SOFTSIZE	2000

	
	TSFileState	m_FileState;
	

	u32			m_FileSize;
	u8 			m_nPacketSize;
	u32 			m_PacketCount;
	u32 	 		Offset;
	
public:
	CTSFile(s8 * pFileName);
	//CTSFile(u8	*	FileData, u32		FileLength);
	~CTSFile(void);
	void		OpenAndAnalyse();
	//bool 		RollBack(TSFileErrorCode reason);
	bool 		Analyse(void);
	bool 		AnalyseAttribute(void);


	bool 		TraceTsfileState();
	void 		ReadPacket(CPacket * pp,u32 PacketIndex);
	//bool 		HardWritePacket(CPacket * pp);
	//bool 		SoftWritePacket(CPacket * pp);
	u8 			GetPacketSize();
    	u32 			GetPacketCount();

	u16 			GetPacketPID(u32 PacketIndex);
	//bool 		SetPacketPID(u32 PacketIndex, u16 NewPid);
	bool 		IsValid(void);
	//bool 		OverWrite(u8 * addr, u32 lenth);
	//bool 		IsOtvMux();
	//bool 		AnalyseOtvHead();
        bool            FindForwardSection(u32 & CurIdx, u16 pid ,u16	tableid);
       CSection *  BuildNextSection(u32 & CurIdx, u16 pid  ,u16	tableid);
	bool            BuildTable(CTable	& pTbl );


};


#endif
